package com.example.ncpapplication.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ncpapplication.R;
import com.example.ncpapplication.bean.NCPInfo;

import java.util.List;

public class MyListViewAdapter extends ArrayAdapter<NCPInfo.NewslistBean.CitiesBean> {

    private int resource;

    public MyListViewAdapter(@NonNull Context context, int resource, @NonNull List<NCPInfo.NewslistBean.CitiesBean> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        NCPInfo.NewslistBean.CitiesBean cities = getItem(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
            holder = new ViewHolder();
            holder.tv_city_cityName = convertView.findViewById(R.id.tv_city_cityName);
            holder.tv_city_confirmedCount = convertView.findViewById(R.id.tv_city_confirmedCount);
            holder.tv_city_suspectedCount = convertView.findViewById(R.id.tv_city_suspectedCount);
            holder.tv_city_curedCount = convertView.findViewById(R.id.tv_city_curedCount);
            holder.tv_city_deadCount = convertView.findViewById(R.id.tv_city_deadCount);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_city_cityName.setText(cities.getCityName());
        holder.tv_city_confirmedCount.setText(cities.getConfirmedCount() + "人");
        holder.tv_city_suspectedCount.setText(cities.getSuspectedCount() + "人");
        holder.tv_city_curedCount.setText(cities.getCuredCount() + "人");
        holder.tv_city_deadCount.setText(cities.getDeadCount() + "人");
        return convertView;
    }

    private class ViewHolder {
        TextView tv_city_cityName, tv_city_confirmedCount, tv_city_suspectedCount, tv_city_curedCount, tv_city_deadCount;
    }

}
